<?php


namespace App\Services;


use App\Models\Department;
use App\Models\User;
use App\Models\Work;
use App\Service\Service;
use Carbon\Carbon;
use Dingo\Api\Http\Request;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;

/**
 * Class StatisticalService
 * @property integer $department_id
 * @property string $start
 * @property string $end
 * @package App\Services
 * @author thanatos thanatos915@163.com
 */
class StatisticalService extends Service
{

    /**
     * 工作数据统计数据
     * @param Request $request
     * @author thanatos thanatos915@163.com
     */
    public static function workData(Request $request)
    {
        $service = self::loadParams($request, [
            'department_id' => 'numeric',
            'start' => 'date',
            'end' => 'date'
        ]);

        $query = Work::query();

        if ($service->start && $service->end) {
            $start = Carbon::createFromTimestamp(strtotime($service->start))->startOfDay();
            $end = Carbon::createFromTimestamp(strtotime($service->end))->endOfDay();
            $query->where('day', '>=', $start)->where('day', '<', $end);
        }

        $department_id = $service->department_id ?: 0;

        // 获取统计总数
        $departments = Department::get();

        $department_ids = static::getChildDepartment($departments, $department_id);

        // 统计综述
        $count = self::getDepartmentData($department_ids, $query);

        $list = [];
        // 获取需要统计的子部门
        foreach ($departments->where('parent_id', $department_id) as $department) {
            // 查询子部门数据
            $child_ids = static::getChildDepartment($departments, $department->id);
            $count_data = static::getDepartmentData($child_ids, $query);
            $count_data['name'] = $department->name;
            array_push($list, $count_data);
        }

        return [
            'count' => $count,
            'list' => $list
        ];
    }

    /**
     * 统计部门中的所有工作数据
     * @param $ids
     * @param Builder $query
     * @return array
     * @author thanatos thanatos915@163.com
     */
    public static function getDepartmentData($ids, $query)
    {
        $usersQuery = User::whereIn('department_id', $ids)->select('id');
        $query->whereIn('user_id', $usersQuery);

        $data = $query->selectRaw('sum(`amount`) as 资料量, sum(`effect_mobile`) as  有效电话,sum(`visit`) as 拜访客户量,sum(`order`) as  成单量,sum(`wechat`) as 微信,sum(`intention`) as  意向客户')
            ->limit(1)->first()->toArray();

        foreach ($data as &$item) {
            $item = intval($item) + 10;
        }
        return $data;
    }

    /**
     * 获取所有子部门
     * @param Collection $departments
     * @param $department_id
     * @return array
     * @author thanatos thanatos915@163.com
     */
    private static function getChildDepartment($departments, $department_id)
    {
        $data = [];
        $child = $departments->where('parent_id', $department_id);
        if ($child->count() > 0) {
            foreach ($child as $item) {
                array_push($data, $item->id);
                $tmp = $departments->where('parent_id', $item->id);
                if ($tmp->count() > 0) {
                    array_push($data, ...static::getChildDepartment($departments, $item->id));
                }
            }
        }
        return $data;
    }

}
